package com.kdtech.analyse.video;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class SinaAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		// http://video.sina.com.cn/p/sports/cba/v/2012-11-25/183661926157.html
		String[] regex = {
				"http://.*.sina.com.cn/.*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]+.html",
					};

		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = 0l;

		Document doc = Jsoup.parse(html);
		/*
		 * 解析标题
		 */
		title = doc.select("div.Vd_titBox h2").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("div#videoTitle").text();
			if (StringUtils.isBlank(title)) {
				title = doc.select("title").text();
				title = StringUtils.substringBefore(title, "_");
			}
		}
		/*
		 * 解析时间
		 */
		date = DateUtils.matchDate(doc.select("div#videoUpdatetime").text());
		if(date==null){
			date = DateUtils.matchDate(doc.select("p.from").text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("p.grey").text());
		}
		/*
		 * 解析内容
		 */
		desc = doc.select("td#videoContent").text();
		if(StringUtils.isBlank(desc)){
			desc = doc.select("em[task=oldinfor]").text();
		}
		if(StringUtils.isBlank(desc)){
			desc = doc.select("div.main-body").text();
		}
		video.setTitle(title);
		video.setDate(date);
		video.setContent(desc);

		if (StringUtils.isBlank(title)) {
		}
		/**
		 * 解析播放次数
		 */
		// http://count.kandian.com/getCount.php?vids=90967887-90967887
		String tmp = StringUtils.substringBetween(html, "vid:'", "'");
		video.setUpdateUrl("http://count.kandian.com/getCount.php?vids="
				+ tmp + "-" + tmp);
		NewsMeta update = Update(video);
		if (update != null) {
			video.setClickNum(update.getClickNum());
		}
		String author=JSoupUtils.matchAuthor(doc, "来源：");
		video.setAuthor(author);
		return video;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				String html = responseToURL.getHtml();
				if (html != null) {
					int pos = html.indexOf("\":\"");
					if (pos != -1) {
						html = html.substring(pos + "\":\"".length());
						pos = html.indexOf("\"}");
						if (pos != -1) {
							html = html.substring(0, pos).replace(",", "");
							try {
								int parseInt = Integer.parseInt(html);
								meta.setClickNum(parseInt);
								return meta;
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
							}
						}
					}
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		SinaAnalyse a = new SinaAnalyse();
		String url = "http://sinanews.sina.cn/sharenews.shtml?id=fxncyar6407361-comos-news-cms&fromsinago=1&page_start=1&page_end=100";
		if (a.isDetailPage(url)) {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不匹配规则");
		}

	}
	

	public static HashMap<String, String> getLinks(String response) {
		HashMap<String, String> tmplinks = new HashMap<String, String>();
		Document doc = Jsoup.parse(response);
		Elements links = doc.select("li");
		for (Element element : links) {
			String vurl = element.attr("vurl");
			if(StringUtils.isNotBlank(vurl)){
				String context = element.attr("vtitle");
				if(StringUtils.isNotBlank(context) && context.length()>100){
					context= context.substring(0,100);
				}
				tmplinks.put(vurl,context);
			}
		}
		links = doc.select("div.news-item");
		for (Element element : links) {
			String vurl = element.attr("data-url");
			if(StringUtils.isNotBlank(vurl)){
				String context = element.attr("data-title");
				if(StringUtils.isNotBlank(context) && context.length()>100){
					context= context.substring(0,100);
				}
				tmplinks.put(vurl,context);
			}
		}
		return tmplinks;
	}
}
